//-----------------------------------------------------------------------
// <copyright file="tblImportTransactionDAOBase.cs" company="Scb">
//     Copyright ? Scb. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using Scb.Framework;
using Scb.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Robot.Entities;

namespace Robot.Data
{    
    public abstract class tblImportTransactionDAOBase
    {
        #region Common methods
        public virtual tblImportTransaction CreatetblImportTransactionFromReader(IDataReader reader)
        {
            tblImportTransaction item = new tblImportTransaction();
            try
            {
                if (UtilityDAO.CheckColumnData(reader,"ID")) item.ID = (Guid)reader["ID"];
                if (UtilityDAO.CheckColumnData(reader,"ValueDate")) item.ValueDate = (DateTime)reader["ValueDate"];
                if (UtilityDAO.CheckColumnData(reader,"BuyCurrency")) item.BuyCurrency = (string)reader["BuyCurrency"];
                if (UtilityDAO.CheckColumnData(reader,"SellCurrency")) item.SellCurrency = (string)reader["SellCurrency"];
                if (UtilityDAO.CheckColumnData(reader,"CusRate")) item.CusRate = (decimal)reader["CusRate"];
                if (UtilityDAO.CheckColumnData(reader,"BuyAmount")) item.BuyAmount = (decimal)reader["BuyAmount"];
                if (UtilityDAO.CheckColumnData(reader,"SellAmount")) item.SellAmount = (decimal)reader["SellAmount"];
                if (UtilityDAO.CheckColumnData(reader,"Branch")) item.Branch = (string)reader["Branch"];
                if (UtilityDAO.CheckColumnData(reader,"Rule1")) item.Rule1 = (int)reader["Rule1"];
                if (UtilityDAO.CheckColumnData(reader,"Rule2")) item.Rule2 = (int)reader["Rule2"];
                if (UtilityDAO.CheckColumnData(reader,"Rule3")) item.Rule3 = (int)reader["Rule3"];
                if (UtilityDAO.CheckColumnData(reader,"Rule4")) item.Rule4 = (int)reader["Rule4"];
                if (UtilityDAO.CheckColumnData(reader,"Rule5")) item.Rule5 = (int)reader["Rule5"];
                if (UtilityDAO.CheckColumnData(reader,"FromFileID")) item.FromFileID = (Guid)reader["FromFileID"];
                if (UtilityDAO.CheckColumnData(reader,"CreatedBy")) item.CreatedBy = (string)reader["CreatedBy"];
                if (UtilityDAO.CheckColumnData(reader,"CustomerName")) item.CustomerName = (string)reader["CustomerName"];
                if (UtilityDAO.CheckColumnData(reader,"CreatedDate")) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (UtilityDAO.CheckColumnData(reader,"ApprovedDate")) item.ApprovedDate = (DateTime)reader["ApprovedDate"];
                if (UtilityDAO.CheckColumnData(reader,"ApprovedBy")) item.ApprovedBy = (string)reader["ApprovedBy"];
                if (UtilityDAO.CheckColumnData(reader,"Channel")) item.Channel = (string)reader["Channel"];
                if (UtilityDAO.CheckColumnData(reader,"BatchNo")) item.BatchNo = (string)reader["BatchNo"];
                if (UtilityDAO.CheckColumnData(reader,"CustMasterNo")) item.CustMasterNo = (string)reader["CustMasterNo"];
                if (UtilityDAO.CheckColumnData(reader,"CusSeg")) item.CusSeg = (string)reader["CusSeg"];
                if (UtilityDAO.CheckColumnData(reader,"Status")) item.Status = (int)reader["Status"];
                if (UtilityDAO.CheckColumnData(reader,"SlNo")) item.SlNo = (string)reader["SlNo"];
                if (UtilityDAO.CheckColumnData(reader,"CustCD")) item.CustCD = (string)reader["CustCD"];
                if (UtilityDAO.CheckColumnData(reader,"TranReferenceNo")) item.TranReferenceNo = (string)reader["TranReferenceNo"];
                if (UtilityDAO.CheckColumnData(reader,"Exception")) item.Exception = (string)reader["Exception"];
                if (UtilityDAO.CheckColumnData(reader,"Command")) item.Command = (string)reader["Command"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblImportTransactionFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreatetblImportTransaction methods
            
        public virtual void CreatetblImportTransaction(tblImportTransaction tblImportTransaction)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblImportTransactionInsert");
                
                database.AddInParameter(dbCommand, "@ValueDate", DbType.DateTime, tblImportTransaction.ValueDate);
                database.AddInParameter(dbCommand, "@BuyCurrency", DbType.AnsiString, tblImportTransaction.BuyCurrency);
                database.AddInParameter(dbCommand, "@SellCurrency", DbType.AnsiString, tblImportTransaction.SellCurrency);
                database.AddInParameter(dbCommand, "@CusRate", DbType.Decimal, tblImportTransaction.CusRate);
                database.AddInParameter(dbCommand, "@BuyAmount", DbType.Decimal, tblImportTransaction.BuyAmount);
                database.AddInParameter(dbCommand, "@SellAmount", DbType.Decimal, tblImportTransaction.SellAmount);
                database.AddInParameter(dbCommand, "@Branch", DbType.AnsiString, tblImportTransaction.Branch);
                database.AddInParameter(dbCommand, "@Rule1", DbType.Int32, tblImportTransaction.Rule1);
                database.AddInParameter(dbCommand, "@Rule2", DbType.Int32, tblImportTransaction.Rule2);
                database.AddInParameter(dbCommand, "@Rule3", DbType.Int32, tblImportTransaction.Rule3);
                database.AddInParameter(dbCommand, "@Rule4", DbType.Int32, tblImportTransaction.Rule4);
                database.AddInParameter(dbCommand, "@Rule5", DbType.Int32, tblImportTransaction.Rule5);
                database.AddInParameter(dbCommand, "@FromFileID", DbType.Guid, tblImportTransaction.FromFileID);
                database.AddInParameter(dbCommand, "@CreatedBy", DbType.AnsiString, tblImportTransaction.CreatedBy);
                database.AddInParameter(dbCommand, "@CustomerName", DbType.AnsiString, tblImportTransaction.CustomerName);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, tblImportTransaction.CreatedDate);
                database.AddInParameter(dbCommand, "@ApprovedDate", DbType.DateTime, tblImportTransaction.ApprovedDate);
                database.AddInParameter(dbCommand, "@ApprovedBy", DbType.AnsiString, tblImportTransaction.ApprovedBy);
                database.AddInParameter(dbCommand, "@Channel", DbType.AnsiString, tblImportTransaction.Channel);
                database.AddInParameter(dbCommand, "@BatchNo", DbType.AnsiString, tblImportTransaction.BatchNo);
                database.AddInParameter(dbCommand, "@CustMasterNo", DbType.AnsiString, tblImportTransaction.CustMasterNo);
                database.AddInParameter(dbCommand, "@CusSeg", DbType.AnsiString, tblImportTransaction.CusSeg);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblImportTransaction.Status);
                database.AddInParameter(dbCommand, "@SlNo", DbType.AnsiString, tblImportTransaction.SlNo);
                database.AddInParameter(dbCommand, "@CustCD", DbType.AnsiString, tblImportTransaction.CustCD);
                database.AddInParameter(dbCommand, "@TranReferenceNo", DbType.AnsiString, tblImportTransaction.TranReferenceNo);
                database.AddInParameter(dbCommand, "@Exception", DbType.AnsiString, tblImportTransaction.Exception);
                database.AddInParameter(dbCommand, "@Command", DbType.AnsiString, tblImportTransaction.Command);
                database.AddOutParameter(dbCommand, "@ID", DbType.Guid, 16);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                tblImportTransaction.ID = (Guid)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreatetblImportTransactionException, ex);
            }
        }

        #endregion

        #region UpdatetblImportTransaction methods
        
        public virtual void UpdatetblImportTransaction(tblImportTransaction tblImportTransaction)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblImportTransactionUpdate");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, tblImportTransaction.ID);
                database.AddInParameter(dbCommand, "@ValueDate", DbType.DateTime, tblImportTransaction.ValueDate);
                database.AddInParameter(dbCommand, "@BuyCurrency", DbType.AnsiString, tblImportTransaction.BuyCurrency);
                database.AddInParameter(dbCommand, "@SellCurrency", DbType.AnsiString, tblImportTransaction.SellCurrency);
                database.AddInParameter(dbCommand, "@CusRate", DbType.Decimal, tblImportTransaction.CusRate);
                database.AddInParameter(dbCommand, "@BuyAmount", DbType.Decimal, tblImportTransaction.BuyAmount);
                database.AddInParameter(dbCommand, "@SellAmount", DbType.Decimal, tblImportTransaction.SellAmount);
                database.AddInParameter(dbCommand, "@Branch", DbType.AnsiString, tblImportTransaction.Branch);
                database.AddInParameter(dbCommand, "@Rule1", DbType.Int32, tblImportTransaction.Rule1);
                database.AddInParameter(dbCommand, "@Rule2", DbType.Int32, tblImportTransaction.Rule2);
                database.AddInParameter(dbCommand, "@Rule3", DbType.Int32, tblImportTransaction.Rule3);
                database.AddInParameter(dbCommand, "@Rule4", DbType.Int32, tblImportTransaction.Rule4);
                database.AddInParameter(dbCommand, "@Rule5", DbType.Int32, tblImportTransaction.Rule5);
                database.AddInParameter(dbCommand, "@FromFileID", DbType.Guid, tblImportTransaction.FromFileID);
                database.AddInParameter(dbCommand, "@CreatedBy", DbType.AnsiString, tblImportTransaction.CreatedBy);
                database.AddInParameter(dbCommand, "@CustomerName", DbType.AnsiString, tblImportTransaction.CustomerName);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, tblImportTransaction.CreatedDate);
                database.AddInParameter(dbCommand, "@ApprovedDate", DbType.DateTime, tblImportTransaction.ApprovedDate);
                database.AddInParameter(dbCommand, "@ApprovedBy", DbType.AnsiString, tblImportTransaction.ApprovedBy);
                database.AddInParameter(dbCommand, "@Channel", DbType.AnsiString, tblImportTransaction.Channel);
                database.AddInParameter(dbCommand, "@BatchNo", DbType.AnsiString, tblImportTransaction.BatchNo);
                database.AddInParameter(dbCommand, "@CustMasterNo", DbType.AnsiString, tblImportTransaction.CustMasterNo);
                database.AddInParameter(dbCommand, "@CusSeg", DbType.AnsiString, tblImportTransaction.CusSeg);
                database.AddInParameter(dbCommand, "@Status", DbType.Int32, tblImportTransaction.Status);
                database.AddInParameter(dbCommand, "@SlNo", DbType.AnsiString, tblImportTransaction.SlNo);
                database.AddInParameter(dbCommand, "@CustCD", DbType.AnsiString, tblImportTransaction.CustCD);
                database.AddInParameter(dbCommand, "@TranReferenceNo", DbType.AnsiString, tblImportTransaction.TranReferenceNo);
                database.AddInParameter(dbCommand, "@Exception", DbType.AnsiString, tblImportTransaction.Exception);
                database.AddInParameter(dbCommand, "@Command", DbType.AnsiString, tblImportTransaction.Command);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdatetblImportTransactionException, ex);
            }
        }
        
        #endregion

        #region DeletetblImportTransaction methods
        public virtual void DeletetblImportTransaction(Guid id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblImportTransactionDelete");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeletetblImportTransactionException, ex);
            }
        }

        #endregion

        #region GettblImportTransaction methods
        
        public virtual tblImportTransaction GettblImportTransaction(Guid id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblImportTransactionGet");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Guid, id);
                
                tblImportTransaction tblImportTransaction =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tblImportTransaction = CreatetblImportTransactionFromReader(reader);
                        reader.Close();
                    }
                }
                return tblImportTransaction;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblImportTransactionException, ex);
            }
        }
        
        #endregion

        #region GettblImportTransactionList methods
        public virtual tblImportTransactionCollection GettblImportTransactionList(tblImportTransactionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("sptblImportTransactionGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                tblImportTransactionCollection tblImportTransactionCollection = new tblImportTransactionCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        tblImportTransaction tblImportTransaction = CreatetblImportTransactionFromReader(reader);
                        tblImportTransactionCollection.Add(tblImportTransaction);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tblImportTransactionCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGettblImportTransactionListException, ex);
            }
        }
        
        public virtual tblImportTransactionCollection GettblImportTransactionList(tblImportTransactionColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GettblImportTransactionList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}